package com.pay.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Date format.
 *
 * @author Maven
 */
public class DateUtil {
	static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	static SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMdd");
	static SimpleDateFormat df3 = new SimpleDateFormat("yyyy/MM/dd");
	static SimpleDateFormat dfTimestamp = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");
	static SimpleDateFormat dfTimestampsecond = new SimpleDateFormat(
			"yyyy-MM-dd hh:mm:ss");
	static SimpleDateFormat dfTimestampsecond24 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat dfTimestampsecond24_2 = new SimpleDateFormat(
			"yyyy-MM-dd_HHmmss");
	static SimpleDateFormat dfYear = new SimpleDateFormat("yyyy");
	static SimpleDateFormat dfCN = new SimpleDateFormat("yyyy年MM月dd日");
	static SimpleDateFormat dfTimestampCN = new SimpleDateFormat(
			"yyyy年MM月dd日 HH:mm:ss");

	public static Timestamp parseDate(String sDate) throws ParseException {
		return new Timestamp((df.parse(sDate)).getTime());
	}

	public static Timestamp parseDate3(String sDate) throws ParseException {
		return new Timestamp((df3.parse(sDate)).getTime());
	}

	public static Date parseDate2(String sDate) throws ParseException {
		return new Date((df2.parse(sDate)).getTime());
	}


	public static Date parseDate4(String sDate) throws ParseException {
		return new Date((df.parse(sDate)).getTime());
	}

	public static Date parseDate5(String sDate) throws ParseException {
		return new Date((dfTimestampsecond24.parse(sDate)).getTime());
	}


	public static Timestamp parseTimestamp(String sDate) throws ParseException {
		return new Timestamp((dfTimestamp.parse(sDate)).getTime());
	}

	public static Timestamp parseTimestampAMPM(String s) throws ParseException {
		String date = s.substring(0, s.indexOf(" "));
		String hour = s.substring(s.indexOf(" "), s.indexOf(":"));
		String minutes = s.substring(s.indexOf(":") + 1, s.indexOf(":") + 3);
		if (s.endsWith("PM"))
			hour = "" + (Integer.parseInt(hour.trim()) + 12);
		return new Timestamp((dfTimestamp.parse(date + " " + hour + ":"
				+ minutes)).getTime());
	}

	public static Timestamp parseYear(String sDate) throws ParseException {
		return new Timestamp((dfYear.parse(sDate)).getTime());
	}

	public static Timestamp parseTimestamp24(String sTimestamp) throws ParseException {
		return new Timestamp((dfTimestampsecond24.parse(sTimestamp)).getTime());
	}

	public static String getCurrentYear2Digits() {
		int year = GregorianCalendar.getInstance().get(Calendar.YEAR);
		return ("" + year).substring(2);
	}

	public static String getCurrentYear4Digits() {
		int year = GregorianCalendar.getInstance().get(Calendar.YEAR);
		return ("" + year);
	}

	public static String formatDate(Timestamp date) {
		if (date == null)
			return "";
		return df.format(date);
	}

	public static String formatDate2(Date  date) {
		if (date == null)
			return "";
		return df2.format(date);
	}

	public static String formatDate2AddDays(Date  date,int days) {
		if (date == null)
			return "";


		  Calendar   calendar   =   Calendar.getInstance();
		  calendar.setTime(date);
		  calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+days);


		return df2.format(calendar.getTime());
	}

	public static String formatDate2(Timestamp date) {
		if (date == null)
			return "";
		return df2.format(date);
	}

	public static String formatDate3(Timestamp date) {
		if (date == null)
			return "";
		return df3.format(date);
	}

	public static String formatTime(Timestamp date) {
		if (date == null)
			return "";
		return dfTimestampsecond.format(date);
	}

	public static String formatTime12(Timestamp date) {
		if (date == null)
			return "";
		return dfTimestamp.format(date);
	}

	public static String formatTime24(Timestamp date) {
		if (date == null)
			return "";
		return dfTimestampsecond24.format(date);
	}

	public static String formatTime24_2(Timestamp date) {
		if (date == null)
			return "";
		return dfTimestampsecond24_2.format(date);
	}

	public static String threeYearsAgo() {
		int currentYear = (Calendar.getInstance()).get(Calendar.YEAR);
		return "" + (currentYear - 3) + "-01" + "-01";
	}

	public static String getCurrentDateStr() {
		Timestamp currentTime = new Timestamp(System.currentTimeMillis());
		String current = formatDate(currentTime);
		return current;
	}

	public static String getCurrentTimeStr() {
		Timestamp currentTime = new Timestamp(System.currentTimeMillis());
		String current = formatTime24(currentTime);
		return current;
	}

	public static String getCurrentTimeStr2() {
		Timestamp currentTime = new Timestamp(System.currentTimeMillis());
		String current = formatTime24_2(currentTime);
		return current;
	}

	public static Timestamp getCurrentTime() {
		Timestamp currentTime = new Timestamp(System.currentTimeMillis());
		return currentTime;
	}

	public static Timestamp getYestodayEndTime() throws ParseException {
		Timestamp yestoday = new Timestamp(System.currentTimeMillis() - 1000*60*60*24);
		String stringOfYestoday = DateUtil.formatDate(yestoday);
		String[] ary = stringOfYestoday.split(" ");
		String dayOfYestoday = ary[0];

		String stringOfYestodayEnd = dayOfYestoday + " 23:59:59";

		Timestamp result = DateUtil.parseTimestamp24(stringOfYestodayEnd);

		return result;
	}

	public static String formatDateCN(Timestamp date) {
		if (date == null)
			return "";
		return dfCN.format(date);
	}

	public static String formatDateTimeCN(Timestamp date) {
		if (date == null)
			return "";
		return dfTimestampCN.format(date);
	}

	public static String getTimeStampNumberFormat(Timestamp formatTime) {
		SimpleDateFormat m_format = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss",
				new Locale("zh", "cn"));
		return m_format.format(formatTime);
	}

	public static String getTimeDifference(Timestamp startTimestamp,
			Timestamp endTimestamp) {
		SimpleDateFormat timeformat = new SimpleDateFormat(
				"yyyy-MM-dd,HH:mm:ss");
		long t1 = 0L;
		long t2 = 0L;
		try {
			t1 = timeformat.parse(getTimeStampNumberFormat(startTimestamp))
					.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		try {
			t2 = timeformat.parse(getTimeStampNumberFormat(endTimestamp))
					.getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理
		// int hours = (int) ((t1 - t2) / 3600000);
		// int minutes = (int) (((t1 - t2) / 1000 - hours * 3600) / 60);
		int seconds = (int) ((t2 - t1) / 1000);
		return "" + seconds + "秒";
	}

	public static String getWeekOfDate(Calendar cal) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

	public static String getWeekOfDate(Date dt) {
		String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }


	public static Date addDayToDate(Date date, int days) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.DAY_OF_MONTH, days);
		return ca.getTime();
	}
	/**
	 * 取得指定日期格式的 字符串
	 * @param Date
	 * @return String
	 */

	public static String formatDate(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}

	public static String firtDayOfCurrentMonth(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return sdf.format(calendar.getTime());
	}

	public static String lastDayOfCurrentMonth(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return sdf.format(calendar.getTime());
	}
	public static Date getNextDate(Date date) {  
		Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, 1);  
        date = calendar.getTime();  
        return date;  
    }  
	public static String formatDate(String date, String format) {
		if (date == null){
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String str = "";
		try {
			str = sdf.format(dfTimestampsecond24.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return str;
	}
	public static void main(String[] args) throws ParseException{
		String tempDate = getCurrentDateStr();
		Date endDate = parseDate4(tempDate);
		Date startDate = addDayToDate(endDate,-30);
		System.out.println("***********************"+tempDate+"&&&&"+formatDate(endDate,"yyyy-MM-dd HH:mm:ss")+"startDate"+formatDate(startDate,"yyyy-MM-dd HH:mm:ss"));
	}
}

